package org.jeecg.modules.online.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.online.auth.entity.OnlAuthPage;
import org.jeecg.modules.online.auth.entity.OnlAuthRelation;
import org.jeecg.modules.online.auth.mapper.OnlAuthPageMapper;
import org.jeecg.modules.online.auth.mapper.OnlAuthRelationMapper;
import org.jeecg.modules.online.auth.service.IOnlAuthPageService;
import org.jeecg.modules.online.auth.util.AuthConstants;
import org.jeecg.modules.online.auth.vo.AuthColumnVO;
import org.jeecg.modules.online.auth.vo.AuthPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: onl_auth_page
 * @Author: jeecg-boot
 * @Date:   2020-07-16
 * @Version: V1.0
 */
@Service("onlAuthPageServiceImpl")
public class OnlAuthPageServiceImpl extends ServiceImpl<OnlAuthPageMapper, OnlAuthPage> implements IOnlAuthPageService {

    @Autowired
    private OnlAuthRelationMapper onlAuthRelationMapper;

    @Override
    public void disableAuthColumn(AuthColumnVO authColumnVO) {
        LambdaUpdateWrapper<OnlAuthPage> updateWrapper = new UpdateWrapper<OnlAuthPage>()
                .lambda()
                .eq(OnlAuthPage::getCgformId, authColumnVO.getCgformId())
                .eq(OnlAuthPage::getCode, authColumnVO.getCode())
                .eq(OnlAuthPage::getType, AuthConstants.COLUMN)
                .set(OnlAuthPage::getStatus, 0);
        this.update(updateWrapper);
    }


    @Transactional
    @Override
    public void enableAuthColumn(AuthColumnVO authColumnVO) {
        /*String cgformId = authColumnVO.getCgformId();
        String code = authColumnVO.getCode();
        switchFormShow(cgformId, code, authColumnVO.isFormShow());
        switchFormEditable(cgformId, code, authColumnVO.isFormEditable());
        switchListShow(cgformId, code, authColumnVO.isListShow());*/
        String cgFormId = authColumnVO.getCgformId();
        String code = authColumnVO.getCode();
        //启动字段权限处理逻辑
        LambdaQueryWrapper<OnlAuthPage> query = new LambdaQueryWrapper<OnlAuthPage>()
                .eq(OnlAuthPage::getCgformId, cgFormId)
                .eq(OnlAuthPage::getCode, code)
                .eq(OnlAuthPage::getType,AuthConstants.COLUMN);
        List<OnlAuthPage> ls = this.list(query);
        if(ls!=null && ls.size()>0){
            //1.查询原权限配置 如果有数据 只需要将其设置为有效即可
            LambdaUpdateWrapper<OnlAuthPage> updateWrapper = new UpdateWrapper<OnlAuthPage>()
                    .lambda()
                    .eq(OnlAuthPage::getCgformId, cgFormId)
                    .eq(OnlAuthPage::getCode, code)
                    .eq(OnlAuthPage::getType,AuthConstants.COLUMN)
                    .set(OnlAuthPage::getStatus, AuthConstants.VALID);
            this.update(updateWrapper);
        }else{
            //2.如果没有数据 需要将所有的checkbox默认选中 即构造3条数据插入
            ls = new ArrayList<OnlAuthPage>();
            ls.add(new OnlAuthPage(cgFormId,code,AuthConstants.LIST,AuthConstants.SHOW));
            ls.add(new OnlAuthPage(cgFormId,code,AuthConstants.FORM,AuthConstants.SHOW));
            ls.add(new OnlAuthPage(cgFormId,code,AuthConstants.FORM,AuthConstants.EDIT));
            this.saveBatch(ls);
        }
    }

    @Override
    public void switchAuthColumn(AuthColumnVO authColumnVO) {
        String cgformId = authColumnVO.getCgformId();
        String code = authColumnVO.getCode();
        int switchFlag = authColumnVO.getSwitchFlag();
        if(switchFlag == AuthConstants.switchListShow){
            this.switchListShow(cgformId, code, authColumnVO.isListShow());
        }else if(switchFlag == AuthConstants.switchFormShow){
            this.switchFormShow(cgformId, code, authColumnVO.isFormShow());
        }else if(switchFlag == AuthConstants.switchFormEditable){
            this.switchFormEditable(cgformId, code, authColumnVO.isFormEditable());
        }
    }

    @Transactional
    @Override
    public void switchFormShow(String cgformId, String code, boolean flag) {
        //int control = 5,page = 5;
        commonSwitch(cgformId, code, AuthConstants.SHOW, AuthConstants.FORM, flag);
    }

    @Transactional
    @Override
    public void switchFormEditable(String cgformId, String code, boolean flag) {
        //int control = 3,page = 5;
        commonSwitch(cgformId, code, AuthConstants.EDIT, AuthConstants.FORM, flag);
    }

    @Transactional
    @Override
    public void switchListShow(String cgformId, String code, boolean flag) {
       // int control = 5,page = 3;
        commonSwitch(cgformId, code, AuthConstants.SHOW, AuthConstants.LIST, flag);

    }

    @Override
    public List<AuthPageVO> queryRoleAuthByFormId(String roleId, String cgformId, int type) {
        return baseMapper.queryRoleAuthByFormId(roleId, cgformId, type);
    }

    @Override
    public List<AuthPageVO> queryRoleDataAuth(String roleId, String cgformId) {
        return baseMapper.queryRoleDataAuth(roleId, cgformId);
    }

    @Override
    public List<AuthPageVO> queryAuthByFormId(String cgformId, int type) {
        if(type == AuthConstants.COLUMN){
            return baseMapper.queryAuthColumnByFormId(cgformId);
        }else{
            return baseMapper.queryAuthButtonByFormId(cgformId);
        }
    }

    @Override
    public List<String> queryRoleNoAuthCode(String cgformId, Integer control, Integer page) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        return baseMapper.queryRoleNoAuthCode(userId,cgformId,control,page,null);
    }

    @Override
    public List<String> queryFormDisabledCode(String cgformId) {
        return queryRoleNoAuthCode(cgformId, AuthConstants.EDIT, AuthConstants.FORM);
    }

    @Override
    public List<String> queryHideCode(String userId, String cgformId, boolean isList) {
        return baseMapper.queryRoleNoAuthCode(userId,cgformId,AuthConstants.SHOW,isList?AuthConstants.LIST:AuthConstants.FORM,null);
    }

    @Override
    public List<String> queryListHideColumn(String userId, String cgformId) {
        return baseMapper.queryRoleNoAuthCode(userId, cgformId, AuthConstants.SHOW, AuthConstants.LIST, AuthConstants.COLUMN);
    }

    @Override
    public List<String> queryFormHideColumn(String userId, String cgformId) {
        return baseMapper.queryRoleNoAuthCode(userId, cgformId,AuthConstants.SHOW,AuthConstants.FORM, AuthConstants.COLUMN);
    }

    @Override
    public List<String> queryFormHideButton(String userId, String cgformId) {
        return baseMapper.queryRoleNoAuthCode(userId, cgformId,AuthConstants.SHOW,AuthConstants.FORM, AuthConstants.BUTTON);
    }

    @Override
    public List<String> queryHideCode(String cgformId, boolean isList) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        return baseMapper.queryRoleNoAuthCode(userId,cgformId,5,isList?3:5,null);
    }

    @Override
    public List<String> queryListHideButton(String userId, String cgformId) {
        if(userId==null){
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            userId = sysUser.getId();
        }
        return baseMapper.queryRoleNoAuthCode(userId, cgformId, AuthConstants.SHOW, AuthConstants.LIST, AuthConstants.BUTTON);
    }

    private void commonSwitch(String cgformId,String code,int control,int page,boolean flag){
        LambdaQueryWrapper<OnlAuthPage> query = new LambdaQueryWrapper<OnlAuthPage>()
                .eq(OnlAuthPage::getCgformId, cgformId)
                .eq(OnlAuthPage::getCode, code)
                .eq(OnlAuthPage::getControl, control)
                .eq(OnlAuthPage::getPage, page)
                .eq(OnlAuthPage::getType,AuthConstants.COLUMN);
        OnlAuthPage auth = baseMapper.selectOne(query);
        if(flag){
            if(auth==null){
                // 需要增加 实体，设置6个字段
                OnlAuthPage entity = new OnlAuthPage();
                entity.setCgformId(cgformId);
                entity.setCode(code);
                entity.setControl(control);
                entity.setPage(page);
                entity.setType(AuthConstants.COLUMN);
                entity.setStatus(AuthConstants.VALID);
                baseMapper.insert(entity);
            }else{
                //判断状态是否有效
                if(auth.getStatus()==0){
                    auth.setStatus(AuthConstants.VALID);
                    baseMapper.updateById(auth);
                }
            }
        }else if(!flag && auth!=null){
            // 需要删除 权限和关系表数据
            String authId = auth.getId();
            baseMapper.deleteById(authId);
            onlAuthRelationMapper.delete(new LambdaQueryWrapper<OnlAuthRelation>().eq(OnlAuthRelation::getAuthId,authId));
        }
    }


}
